package com.facebank.usersupport.controller;

import com.facebank.usersupport.mapper.usersupport.usersupport.RouteMapper;
import com.facebank.usersupport.model.RestModel;
import com.facebank.usersupport.model.Route;
import com.facebank.usersupport.service.IRouteService;
import com.facebank.usersupport.util.ImgSaveUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
public class RouteController {
    @Autowired
    private IRouteService routeService;

    //TODO 增加路线
    @RequestMapping("/route/add")
    public RestModel add(Route route, @RequestParam(value = "file", required = false) MultipartFile file) {
        String path = "static/images/upload/";
        if (file != null) {
            String targetImgName = ImgSaveUtil.uploadImg(file, path);
            route.setImg("/images/upload/"+targetImgName);
        }
        return routeService.add(route);
    }

    // 线路列表
    @RequestMapping("/route/list")
    public RestModel list(@RequestParam(required = false, defaultValue = "1") int pageNo,
                          @RequestParam(required = false, defaultValue = "6") int length,
                          String routeName, String routeType, String starttime, String endtime) {
//        System.out.println(pageNo + "==" + length + "==" + routeName + "==" + routeType + "==" + starttime + "==" + endtime);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date time1 = null;
        long startTime = 0, endTime = 0;
        if (starttime != "" && starttime != null) {
            try {
                time1 = sdf.parse(starttime);
                startTime = time1.getTime();
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        Date time2 = null;
        if (endtime != "" && endtime != null) {
            try {
                time2 = sdf.parse(endtime);
                endTime = time2.getTime();
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return routeService.list(pageNo, length, routeName, routeType, startTime, endTime);
    }

    // 线路详情
    @RequestMapping("/route/detail")
    public RestModel detail(Integer id) {
        return routeService.detail(id);
    }

    // 删除路线
    @RequestMapping("/route/delete")
    public RestModel delete(Integer id) {
        return routeService.delete(id);
    }

    // 修改路线
    @RequestMapping("/route/updata")
    public RestModel updata(Route route, @RequestParam(value = "file", required = false) MultipartFile file) {
        String path = "static/images/upload/";
        if (file != null) {
            String targetImgName = ImgSaveUtil.uploadImg(file, path);
            route.setImg("/images/upload/"+targetImgName);
        }
        return routeService.updata(route);
    }

    //TODO 路线搜索
    @RequestMapping("/route/search")
    public RestModel search() {
        return new RestModel();
    }
}
